#Conditionally import wlstModule only when script is executed with jython
if __name__ == '__main__': 
    from wlstModule import *#@UnusedWildImport

def getRunningServerNames():
    domainConfig()
    return cmo.getServers()

def monitorRunningServers():
    url='${WLST_AS_URL}'    
    # storeUserConfig('C:/Users/koy/aaworkspace/afls/nodemanger/properties/test/weblogic.secure','C:/Users/koy/aaworkspace/afls/nodemanger/properties/test/weblogic.key') 
    connect(userConfigFile='${ANT_NM_HOME}/security/${env}/weblogic.secure', userKeyFile='${ANT_NM_HOME}/security/${env}/weblogic.key', url='${WLST_AS_URL}')
    edit()
    startEdit()
    
    cd('/')
    cmo.createUnixMachine('${ANT_HOSTNAME}')

    cd('/Machines/${ANT_HOSTNAME}/NodeManager/${ANT_HOSTNAME}')
    cmo.setNMType('Plain')
    cmo.setListenPort(${ANT_NM_PORT})
    cmo.setListenAddress('${ANT_NM_IP}')
    cmo.setDebugEnabled(false)
    
    servers_arr = [${ANT_SERVER_NAMES}]
    for server in servers_arr.iteritems():
        cd('/Servers/' + server)
        cmo.setMachine(getMBean('/Machines/${ANT_HOSTNAME}'))   
    
    try:
        save()
        activate(block="true")
        print "script returns SUCCESS"   
    except Exception, e:
        print e 
        print "Error while trying to save and/or activate!!!"
        undo()        
        raise 
    


if __name__== "main":
    redirect("createMachine.log","false")
    monitorRunningServers()
    stopRedirect()    
